"""
auth:xgt-python
datetime :2021/11/06
某宝电商数据采集
"""

from selenium import webdriver
import time
import random
import csv  # 数据保存模块
from constants import TAO_USENAME,TAO_PASSWORD


def search_produce(keyword):
    """搜索商品数据,以及登录用户"""
    driver.find_element_by_xpath('//*[@id="q"]').send_keys(keyword)
    time.sleep(random.randint(1,3))  # 尽量避免人机检测 随机延迟

    driver.find_element_by_xpath('//*[@id="J_TSearchForm"]/div[1]/button').click()
    time.sleep(random.randint(1,3))  # 尽量避免人机检测  随机延迟

    # 登录
    driver.find_element_by_xpath('//*[@id="fm-login-id"]').send_keys(TAO_USENAME)
    time.sleep(random.randint(1,3))  # 尽量避免人机检测   随机延迟

    driver.find_element_by_xpath('//*[@id="fm-login-password"]').send_keys(TAO_PASSWORD)
    time.sleep(random.randint(1,3))  # 尽量避免人机检测   随机延迟

    driver.find_element_by_xpath('//*[@id="login-form"]/div[4]/button').click()
    time.sleep(random.randint(1,3))  # 尽量避免人机检测   随机延迟


def parse_data():
    """解析商品数据"""
    divs = driver.find_elements_by_xpath('//div[@class="grid g-clearfix"]/div/div') # 所有的div标签

    for div in divs:
        info = div.find_element_by_xpath('.//div[@class="row row-2 title"]/a').text
        price = div.find_element_by_xpath('.//strong').text + '元'
        deal = div.find_element_by_xpath('.//div[@class="deal-cnt"]').text
        name = div.find_element_by_xpath('.//div[@class="shop"]/a/span[2]').text
        location = div.find_element_by_xpath('.//div[@class="location"]').text
        detail_url = div.find_element_by_xpath('.//div[@class="pic"]/a').get_attribute('href')

        print(info,price,deal,name,location,detail_url)

        # 数据保存
        with open('淘宝数据.csv',mode='a' ,encoding='utf-8' ,newline='') as file:  # as file 代码风格借鉴骆昊老师的，yyds
            csv_write = csv.writer(file)
            csv_write.writerow([info,price,deal,name,location,detail_url])





word = input('请输入你要搜素商品的关键字:')
# 创建一个浏览器
driver = webdriver.Chrome()

# selenium操作的浏览器被识别了，无法登录
# 修改浏览器的部分属性，绕过检测
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",
                       {"source":"""Object.defineProperty(navigator,'webdriver',{get: () => false})"""})

# 执行自动化操作
driver.get('https://www.taobao.com/')
driver.implicitly_wait(10) # 设置浏览器等待
driver.maximize_window()  # 最大化浏览器

# 调用商品搜索的函数
search_produce(word)

# 解析商品数据
parse_data()

